
import { Request_API_CN } from 'index.js'
import regeneratorRuntime from '../plugins/regenerator-runtime.js'

export const ToastIcon = (text,icon,date)=>{//模态弹窗
  wx.showToast({
    title: text,
    icon: icon,
    duration: date
  })
}

export const DepositDate = (num)=>{
  //点击开始计时
  //再次调用停止计时
  //参数为超过多少秒才调用成功
  var getTime = wx.getStorageSync('Time')
  const Time = new Date().getTime();

  if (!getTime){//

    wx.setStorageSync('Time', Time)

  }
  else{
    let CalTime = Math.round((Time - getTime) / 1000)

    if (num < CalTime){//如果时间大于

      console.log('可以增加金币')

    }else{

      ToastIcon('必须超过'+num+'才能增加金币','none',2000)

    }
    console.log("当前时间：" + Time)
    console.log("保存的时间：" + getTime)
    console.log("秒数：" + Math.round((Time - getTime) / 1000))

    wx.removeStorageSync('Time')//数据初始化

  }
 
}




export const UserInfo = (t, name, app)=>{//授权登陆
//判断用户是否授权
//数据存储在缓存“user”、
//授权则为true
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.userInfo']) {
        wx.setStorageSync('user', false)
        // console.log('没有登陆')
        
      } else {
        wx.setStorageSync('user', true)
       
        wx.getUserInfo({
          success: (res) => {
            // console.log('登陆提交用户信息')
            getuserinfo(res.userInfo,name,app)
          }
        })
      }
    }
  })
  
}




export const getuserinfo = (e,name,app) => {//提交用户信息
  //参数为小程序ID，例：'user22'
  let obj = {}
  obj.openid = app.globalData.openid;
  obj.nickname = e.nickName;
  obj.gender = e.gender;
  obj.city = e.city;
  obj.province = e.province;
  obj.avatar = e.avatarUrl;
  obj.aid = app.globalData.AID ? app.globalData.AID : '';
  obj.tablename = name;
  wx.setStorageSync('userInfo', obj)
  Request_API_CN('/currency/firstLogin', 'POST', { openid: app.globalData.openid, tablename: name })
    .then((res) => {
      // console.log(res)
      if (res.data.data.isFirstLogin == 1) {
        Request_API_CN('/currency/userInfo', 'POST', obj)
        Request_API_COM('/share/authorize_number', 'POST', { SID: app.globalData.SID })//通过指定分享进入人数(新增)
      }
    })
}

export const shareNumber = (e) => {
  // console.log(e)
  if (e.SID && typeof e.SID != undefined && !wx.getStorageSync('SID')) {//访客次数
    Request_API_COM('/share/addVisitorPerson', 'POST', { SID: e.SID })
      .then((res) => {
        if (res.data.data.code == 1) {
          console.log('通过分享进入人数')
          wx.setStorageSync('SID', e.SID)
        }
      })
  }
  return e
}



export const setInc = (e, id, tp) => {
  if (e.GOpenid) {
    Request_API_COM('/program_census/setInc', 'POST', { appid: id, type: tp })
      .then((res) => {
        tp == 'shareNum' ? console.log('分享次数') : (tp == 'sharePerson' ? console.log('分享人数') : tp == 'sharedPerson' ? console.log('通过分享新增') : "")
      })
  }

}






export const textSlice = (text) => {//数组分割字符串（盒子专属）
  let Faith = text
  let obj = {}
  Faith.split('&').map((item, index) => {
    let name = item.slice(0, item.indexOf('='))
    let text = item.slice(item.indexOf('=') + 1, item.length - item.indexOf('=') + item.indexOf('=') + 1)
    obj[name] = text
    // console.log(obj)
  })
  return obj
}



